import * as actionTypes from './actionTypes'
import { fromJS } from 'immutable'
const defaultState = fromJS({
  focused: false,
  list: [],
  page: 1,
  totalPage: 1,
  mouseIn: false
})

export default (state = defaultState, action) => {
  console.log(state)
  switch(action.type){
    case actionTypes.SEARCH_FOCUS:
      return state.set('focused', true)
    case actionTypes.SEARCH_BLUR:
      return state.set('focused', false)
    case actionTypes.CHANGE_LIST:
      // 一直set太长了
      return state.merge({
        list: action.data,
        totalPage: action.totalPage
      })
    case actionTypes.MOUSE_ENTER:
      return state.set('mouseIn', true)
    case actionTypes.MOUSE_LEAVE:
      return state.set('mouseIn', false)
    case actionTypes.PAGE_CHANGE:
      var page = state.get('page')
      var totalPage = state.get('totalPage')
      if(page < totalPage){
        return state.set('page', ++page)
      }else{
        return state.set('page', 1)
      }
    default:
      return state;
  }
}